/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.claro.administracionSolicitudes.dao;

import co.com.claro.administracionSolicitudes.dao.exceptions.NonexistentEntityException;
import co.com.claro.administracionSolicitudes.dao.exceptions.PreexistingEntityException;
import co.com.claro.administracionSolicitudes.dao.exceptions.RollbackFailureException;
import co.com.claro.administracionSolicitudes.entity.HistoricoSolicitud;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Mike
 */
public class HistoricoSolicitudJpaController implements Serializable {

    public HistoricoSolicitudJpaController() {

    }
    
     @PersistenceContext
    private EntityManager em;
     
    public void create(HistoricoSolicitud historicoSolicitud) throws PreexistingEntityException, RollbackFailureException, Exception {
        try {
            em.persist(historicoSolicitud);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public void edit(HistoricoSolicitud historicoSolicitud) throws NonexistentEntityException, RollbackFailureException, Exception {
        
        try {
            historicoSolicitud = em.merge(historicoSolicitud);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        
        try {
            HistoricoSolicitud historicoSolicitud;
            try {
                historicoSolicitud = em.getReference(HistoricoSolicitud.class, id);
                historicoSolicitud.getIdHistSolicitud();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historicoSolicitud with id " + id + " no longer exists.", enfe);
            }
            em.remove(historicoSolicitud);
            
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<HistoricoSolicitud> findHistoricoSolicitudEntities() {
        return findHistoricoSolicitudEntities(true, -1, -1);
    }

    public List<HistoricoSolicitud> findHistoricoSolicitudEntities(int maxResults, int firstResult) {
        return findHistoricoSolicitudEntities(false, maxResults, firstResult);
    }

    private List<HistoricoSolicitud> findHistoricoSolicitudEntities(boolean all, int maxResults, int firstResult) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(HistoricoSolicitud.class));
        Query q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    }

    public HistoricoSolicitud findHistoricoSolicitud(Integer id) {
        return em.find(HistoricoSolicitud.class, id);
    }

    public int getHistoricoSolicitudCount() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<HistoricoSolicitud> rt = cq.from(HistoricoSolicitud.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
}
